package com.twu.biblioteca;

public class BibliotecaApp {

    private Console console;
    private Library library;
    private UserInterface userInterface;


    public BibliotecaApp(Console console, Library library) {
        this.console = console;
        this.library = library;
        this.userInterface = new NormalUserInterface();
    }

    public void run() {
        int choice;

        displayWelcomeMessage();

        do {
            displayMenu();

            choice = userInterface.getUserChoice();

            userInterface.processRequest(this, choice);

        }while (choice != userInterface.quit());

    }

    public void listBooks() {
        library.getBookList().displaybooks();
    }

    public void listMovies() {
        library.getMovieList().displayMovies();
    }

    public void checkOutBook() {
        BookDetail book;
        try {
            book = searchBook(console.readString("\nEnter name..."));
        } catch (Exception e) {
            logUnsuccessfulCheckout("book");
            return;
        }

        try {
            library.checkOut(book);
        } catch (Exception e) {
            logUnsuccessfulCheckout("book");
            return;
        }
        logSuccessfulCheckout("book");
    }


    public void returnBook() {
        BookDetail book;
        try {
            book = searchBook(console.readString("\nEnter name..."));
        } catch (Exception e) {
            logUnsuccessfulCheckout("book");
            return;
        }

        try {
            library.returnCheckedOutItem(book);
        } catch (Exception e) {
            logUnsuccessfulReturn("book");
            return;
        }

        logSuccessfulReturn("book");
    }

    public void checkOutMovie() {
        MovieDetail movie;
        try {
            movie = searchMovie(console.readString("\nEnter name..."));
        } catch (Exception e) {
            logUnsuccessfulCheckout("movie");
            return;
        }
        try {
            library.checkOut(movie);
        } catch (Exception e) {
            logUnsuccessfulCheckout("movie");
            return;
        }
        logSuccessfulCheckout("movie");
    }


    public void returnMovie() {
        MovieDetail movie;
        try {
            movie = searchMovie(console.readString("\nEnter name..."));
        } catch (Exception e) {
            logUnsuccessfulCheckout("movie");
            return;
        }

        try {
            library.returnCheckedOutItem(movie);
        } catch (Exception e) {
            logUnsuccessfulCheckout("movie");
            return;
        }

        logSuccessfulReturn("movie");
    }

    private void displayMenu() {
        userInterface.displayMenu();
    }

    private void displayWelcomeMessage() {
        userInterface.displayWelcomeMessage(library.getName());
    }


    private void logSuccessfulReturn(String itemName) {
        console.write("\nThank you for returning the " + itemName + ".\n");
    }

    private void logUnsuccessfulReturn(String itemName) {
        console.write("\nThat is not a valid " + itemName + " to return.\n");
    }

    private void logSuccessfulCheckout(String itemName) {
        console.write("\n Thank You! Enjoy the " + itemName + ".\n");
    }

    private void logUnsuccessfulCheckout(String itemName) {
        console.write("\n That " + itemName + " is not available.\n");
    }

    private BookDetail searchBook(String bookName) throws Exception {
        BookDetail book;
        try {
            book = library.getBookList().searchBook(bookName);
        } catch (Exception e) {
            throw e;
        }

        return book;
    }

    private MovieDetail searchMovie(String movieName) throws Exception {
        MovieDetail movie;
        try {
            movie = library.getMovieList().searchMovie(movieName);
        } catch (Exception e) {
            throw e;
        }

        return movie;
    }


    public void login() {
        try {
            library.login();
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.userInterface = new LoggedUserInterface();
    }

    public void viewProfile() {
        UserList members = library.getMembers();
        UserDetail user = null;
        try {
            user = members.search(members.getCurrentUserId());
        } catch (Exception e) {
            e.printStackTrace();
        }

        console.write(user.toString());
    }
}

